# -*- coding:utf-8 -*-

# @Time  : 2024/4/3 2:25 PM
# @Author: chenyong
# @Email : chenyong@lingxi.ai
# @File  : standard_terms_util.py

import pandas as pd

class StandardTerms():
    def __init__(self, terms_file_path):
        self.terms_file_path = terms_file_path
        self.question2terms = self.get_terms()

    def get_terms(self):
        df = pd.read_excel(self.terms_file_path, keep_default_na=False)
        result = {} #{产品名:{标准条款名:条款内容}}
        product_names = df.columns[3:]
        for product in product_names:
            product = product.strip()
            product_dict = {}
            for _, row in df.iterrows():
                clause = row[0]
                if clause == "产品简介":
                    continue
                clause_detail = row[product]
                if clause_detail != '':
                    product_dict[clause] = "保险产品:" + product + '\n' + "产品条款:" + clause + '\n' + clause_detail.strip()
            result[product] = product_dict

        return result


    def get_terms_by_question(self, standard_question, product_name):
        if product_name in self.question2terms:
            question2terms = self.question2terms[product_name]
            if standard_question in question2terms:
                return question2terms[standard_question]
        return ""


if __name__ == "__main__":
    path = "../doc/用户问题-答疑解惑.xlsx"
    st = StandardTerms(path)
    question = "产品简介"
    product = "长相安长期医疗险（20年保证续保）-个人版"
    print(st.get_terms_by_question(question, product))
